import { systemDate, deptTree, currentYearTotalRevenue, currentYearTotalGrossProfit, currentYearSubitemlRevenue, currentYearTotalCost, briefing, costStatisticsByPie, revenueStatistics, costStatisticsByPillar, costRevenueRatio, briefingDetail } from "@/api/system/largeScreen"
import { reactive, toRefs } from "vue"
const data = reactive({
    systemTime: '',
    companyList: [],
    treeLData: {},
    briefData: null,
    pieData: null,
    Doughnut: null,
    barT: null,
    barB: null,
    ratios: null,
    barChart: null,
    transData: {},
    tabname: 'digital',
    briefDataDetail: null
})
export function useHome() {
    // 获取系统时间
    const getSystemTime = async () => {
        // const { msg } = await systemDate()
        // data.systemTime = msg
        let formattedTime = '';
        const now = new Date();
        const year = now.getFullYear();
        const month = String(now.getMonth() + 1).padStart(2, '0');
        const day = String(now.getDate()).padStart(2, '0');
        const hours = String(now.getHours()).padStart(2, '0');
        const minutes = String(now.getMinutes()).padStart(2, '0');
        const seconds = String(now.getSeconds()).padStart(2, '0');
        formattedTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
        data.systemTime = formattedTime

    }

    // 公司树
    const getDeptTree = async () => {
        const res = await deptTree();
        if (res?.data?.length) {
            data.companyList = res.data;
        }
    }

    // 左侧部分数据
    const getTotalData = async (deptId: any) => {
        Promise.all([
            await currentYearTotalRevenue(deptId),
            await currentYearSubitemlRevenue(deptId),
            await currentYearTotalCost(deptId),
            await currentYearTotalGrossProfit(deptId),
        ]).then((res: any) => {
            console.log(res)
            data.treeLData = {
                '1': res[0].data,
                '2': res[1].data.list,
                '3': res[2].data,
                '4': res[3].data
            }
        })
    }

    // 获取简介数据
    const getBriefData = async() => {
        const res = await briefing()
        data.briefData = res.data
    }

    // 获取简介详情
    const getBriefingDetail = async(id) => {
        const res = await briefingDetail(id)
        data.briefDataDetail = res.msg
    }

    // pie图数据
    const getPieData = async(deptId: any) => {
        const res = await costStatisticsByPie(deptId)
        data.pieData = res.data.list
    }

    // 上柱
    const revenueStatisticsData = async(deptId: any) => {
        const res = await revenueStatistics(deptId)
        data.barT = res.data
    }

    // 下柱
    const costStatisticsByPillarData = async(deptId: any) => {
        const res = await costStatisticsByPillar(deptId)
        data.barB = res.data
    }

    // 比例
    const costRevenueRatioData = async(deptId: any) => {
        const res = await costRevenueRatio(deptId)
        data.ratios = res.data
    }
    return {
        ...toRefs(data),
        getSystemTime,
        getDeptTree,
        getTotalData,
        getBriefData,
        getPieData,
        revenueStatisticsData,
        costStatisticsByPillarData,
        costRevenueRatioData,
        getBriefingDetail
    }
}
